<?php
/**
 * @copyright meila.com
 * @author shaopu@
 * @name
 * @param
 *         $xxx = 作用
 * @static
 *         XXX = 作用
 *
 *
 */

class ml_model_rdsLbFilter extends ml_model_redis
{
    const SINGLE_KEY_PREFFIX = 'lmt_';
    const COLLECT_KEY_PREFFIX = 'lmf_';
    function __construct() {
        if(!$this->init_rds('lb_filter'))
            return false;
    }

    public function get_filter_result($class_id , $arr_tagid , $page , $pagesize = 16)
    {
        $this->_data = null;
        $arr_rdsKey = array();
        $destKey = self::COLLECT_KEY_PREFFIX.$class_id.'_';
        if(!is_array($arr_tagid) || count($arr_tagid))
        {
            //按从小到大排个序尽量使用缓存
            sort($arr_tagid);

            foreach ($arr_tagid as $tag_id)
            {
                $arr_rdsKey[] = self::SINGLE_KEY_PREFFIX.$class_id.'_'.$tag_id;
                $destKey .= base_convert($tag_id , 10 , 36);
            }
        }

        //对多个标签进行交集操作
        $rs = $this->oRedis->zInter($destKey , $arr_rdsKey);
        if(!$rs)
            return true;


        $start = ($page - 1) * $pagesize;
        $end = $start + 15;

        $this->_data = array(
            'total' => $this->oRedis->zSize($destKey),
            'pic_ids' => $this->oRedis->zRange($destKey , $start , $end)
        );

        return true;
    }

    public function getCountByTgid($class_id,$tag_id) {
        $key = SINGLE_KEY_PREFFIX.$class_id.'_'.$tag_id;
        return $this->oRds->zSize($key);
    }
}
?>